<?php
	if($user->isLogged()) {		
		$tempuser = new user();
		$errors = array();	
		if(isset($_REQUEST['submit'])) {
			if($user->validateField("name",$_POST['name'])) {	
				$user->name = $_POST['name'];
			} else {	
				$errors['name'] = "Nome non valido!";
			}	
			if($user->validateField("surname",$_POST['surname'])) {	
				$user->surname = $_POST['surname'];
			} else {	
				$errors['surname'] = "Cognome non valido!";
			}	
			if($user->validateField("phone",$_POST['phone'])) {	
				$user->phone = $_POST['phone'];
			} else {	
				$errors['phone'] = "Numero telefonico non valido!";
			}	
			if($user->validateField("address",$_POST['address'])) {	
					$user->address = $_POST['address'];
			} else {	
				$errors['address'] = "Indirizzo non valido!";
			}	
			if($user->validateField("city",$_POST['city'])) {	
				$user->city_id = $_POST['city'];
			} else {	
				$errors['city'] = "Località non valida!";
			}
		}	
		if(isset($_REQUEST['submit']) && count($errors)==0) {
			if(!$user->toDB($user->id)) {
				echo "<script>alert('Errore nell\'aggiornamento dell\'utente!')</script>";
			} else {
				echo "<script>alert('Utente aggiornato correttamente!')</script>";			}
		}	
			
		if(isset($_REQUEST['submitPassword'])) {
			if($_POST['oldPassword'] == "") {				
				$errors['oldPassword'] = "Inserisci l'attuale password dell'account!";
			} else if($user->password != md5($_POST['oldPassword'])) {				
				$errors['oldPassword'] = "L'attuale password dell'account non &egrave; esatta!";
			}	
			if($_POST['newPassword'] == "") {
				$errors['newPassword'] = "Inserisci la password desiderata!";
			} else {	
				if($_POST['newPassword'] != $_POST['newRpassword'] OR $_POST['newRpassword'] == "") {	
					$errors['newPassword'] = "Le due password non corrispondono!";
				} else {	
					$oldPassword = $user->password;
					$user->password = md5($_POST['newPassword']);
				}
			}
		}	
		if(isset($_REQUEST['submitPassword']) && count($errors)==0) {
			if(!$user->toDB($user->id)) {
				echo "<script>alert('Errore nell\'aggiornamento dell\'utente')</script>";
			} else {
				echo "<script>alert('Utente aggiornato correttamente!')</script>";
			}
		}
?>	
		<div id="quote">
			<h3>Dati personali - Indirizzo fatturazione</h3>
			<form action="#" method="post">
				<table>
					<tr>
						<td class="desc">email *</td>
						<td><input type="text" name="email" disabled="disabled" value="<?php echo $user->email; ?>"></td>
						<td class="error"><?php if(isset($errors['email'])) { echo $errors['email']; } ?></td>
					</tr>
					<tr>		
						<td colspan="3">&nbsp;</td>
					</tr>
					<tr>
						<td class="desc">nome *</td>
						<td><input type="text" name="name" value="<?php echo $user->name; ?>"></td>
						<td class="error"><?php if(isset($errors['name'])) { echo $errors['name']; } ?></td>
					</tr>	
					<tr>
						<td class="desc">cognome *</td>
						<td><input type="text" name="surname" value="<?php echo $user->surname; ?>"></td>
						<td class="error"><?php if(isset($errors['surname'])) { echo $errors['surname']; } ?></td>
					</tr>	
					<tr>
						<td class="desc">telefono *</td>
						<td><input type="text" name="phone" value="<?php echo $user->phone; ?>"></td>
						<td class="error"><?php if(isset($errors['phone'])) { echo $errors['phone']; } ?></td>
					</tr>	
					<tr>
						<td colspan="3">&nbsp;</td>
					</tr>
					<tr>
						<td class="desc">indirizzo *</td>
						<td><input type="text" name="address" value="<?php echo $user->address; ?>"></td>
						<td class="error"><?php if(isset($errors['address'])) { echo $errors['address']; } ?></td>
					</tr>	
					<tr>
						<td class="desc">localit&agrave; *</td>
						<td>
							<select name="city">
								<?php
									$cities = mysql_query("SELECT * FROM `city` ORDER BY `name`");
									while($city = mysql_fetch_array($cities)) {
									if($city['ID'] == $user->city_id) { 
										echo '<option value="'.$city['ID'].'" selected>';
									} else {
										echo '<option value="'.$city['ID'].'">';
									}	
											echo $city['name'];
										echo '</option>';
									}
								?>
							</select>
						<td class="error"><?php if(isset($errors['city'])) { echo $errors['city']; } ?></td>
					</tr>
					<tr>		
						<td colspan="3">&nbsp;</td>
					</tr>
					<tr>
						<td class="desc">&nbsp;</td>					
						<td colspan="2"><input type="submit" name="submit" value="Modifica dati"></td>
					</tr>					<tr>						<td colspan="3">Tutti i campi segnati con * sono obbligatori!</td>					</tr>
				</table>
			</form>
		</div>		
		<div id="quote">
			<h3>Password</h3>
			<form action="#" method="post">
				<table>
					<tr>
						<td class="desc">vecchia password *</td>
						<td><input type="password" name="oldPassword"></td>
						<td class="error"><?php if(isset($errors['oldPassword'])) { echo $errors['oldPassword']; } else if(count($errors)>0 && isset($_POST['submitPassword'])) { echo "Inserisci nuovamente l'attuale password!"; } ?></td>
					</tr>	
					<tr>
						<td class="desc">nuova password *</td>
						<td><input type="password" name="newPassword"></td>
						<td class="error"><?php if(isset($errors['newPassword'])) { echo $errors['newPassword']; } else if(count($errors)>0 && isset($_POST['submitPassword'])) { echo "Inserisci nuovamente la password desiderata!"; } ?></td>
					</tr>	
					<tr>
						<td class="desc">ripeti nuova password *</td>
						<td><input type="password" name="newRpassword"></td>
						<td class="error"></td>
					</tr>
					<tr>		
						<td colspan="3">&nbsp;</td>
					</tr>
					<tr>						<td class="desc">&nbsp;</td>											<td colspan="2"><input type="submit" name="submitPassword" value="Modifica Password"></td>					</tr>					<tr>						<td colspan="3">Tutti i campi segnati con * sono obbligatori!</td>					</tr>
				</table>
			</form>
		</div>
<?php	
	} else {
		echo 'Effettuare l\'accesso';
	}?>